<template>
	<view class="apply" style="padding: 20rpx;">
		<view class="type" style="display: flex;line-height: 80rpx;">
			<span class="title">请假类型：</span>
			<uni-data-select v-model="type" :localdata="types" @change="change"></uni-data-select>
		</view>
		<view class="startTime" style="display: flex;line-height: 80rpx;margin-top: 40rpx;">
			<span class="title">开始时间：</span>
			<uni-datetime-picker type="date" v-model="startTime" @change="changeTime" />
		</view>
		<view class="endTime" style="display: flex;line-height: 80rpx;margin-top: 40rpx;">
			<span class="title">结束时间：</span>
			<uni-datetime-picker type="date" v-model="endTime" @change="changeTime" />
		</view>
		<view class="approverUser" style="display: flex;line-height: 80rpx;margin-top: 40rpx">
			<span class="title">审批人：</span>
			<uni-data-select v-model="approverUser" :localdata="approverUsers" @change="change"></uni-data-select>
		</view>
		<view class="btn-submit" @click="submit">
			提交
		</view>
	</view>
</template>

<script>
	import {
		lookApprover,
		addLeave
	} from '@/api/leave.js'
	export default {
		data() {
			return {
				type: null,
				types: [{
						value: 0,
						text: '事假'
					},
					{
						value: 1,
						text: '病假'
					}
				],
				startTime: null,
				endTime: null,
				approverUser: null,
				approverUsers: [],
				userInfo: null
			}
		},
		created() {
			this.init()
		},
		methods: {
			async init() {
				// this.approverUsers = JSON.parse(uni.getStorageSync('approverUser')).filter(item=>{})
				this.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
				const res = await lookApprover()
				if (res.status == 0) {
					res.data.forEach(item => {
						if (item.town_id == this.userInfo.town_id) {
							const obj = {
								value: item.id,
								text: item.name
							}
							this.approverUsers.push(obj)
						}
					})
				}
			},
			change() {
				// console.log(this.type);
			},
			changeTime() {
				// console.log(this.startTime, this.endTime);
			},
			async submit() {
				if (this.startTime > this.endTime) {
					uni.showModal({
						title: '提示',
						content: '开始时间必须在结束之前',
					})
				} else {
					const that = this
					const obj = {
						type: this.type, // 请假类型
						startTime: this.startTime, //请假开始时间
						endTime: this.endTime, // 请假结束时间
						user_id: this.userInfo.id, // 请假人id
						approver_user_id: this.approverUser, // 审批人id
						approver_id: this.userInfo.approver_id, // 审批组id
					}
					const res = await addLeave(obj)
					if (res.status == 0) {
						uni.switchTab({
							url: '/pages/leave/leave'
						})


					}
				}
			}
		}
	}
</script>

<style lang="less" scoped>
	.apply {
		position: relative;
	}

	.title {
		width: 20%;
	}

	.btn-submit {
		width: 100%;
		background-color: aqua;
		position: fixed;
		padding: 0 !important;
		// bottom: 20rpx;
		bottom: 0;
		text-align: center;
		line-height: 80rpx;
		font-size: 48rpx;
		color: #ffffff;
		height: 80rpx;
		left: 0;
	}
</style>
